/*-------------------------------------------------------------------------------------------------*/
/* Nuvoton Corporation confidential                                                    */
/*                                                                                                 */
/* Copyright (c) 2009 by Nuvoton Corporation                                           */
/* All rights reserved                                                                             */
/*                                                                                                 */
/*-------------------------------------------------------------------------------------------------*/
/* File Name:                                                                                      */
/*     smartcard_test.c                                                                            */
/* Description:                                                                                    */
/*     Smartcard test program                                                                      */
/* History:                                                                                        */
/*                                                                                                 */
/* Project:                                                                                        */
/*-------------------------------------------------------------------------------------------------*/

#include "wblib.h"
#include "NUC900_smartcard.h"


static INT _ScTest(INT nCardNo);



/****************************************************************************
* FUNCTION
*   _ScHelp
*
* DESCRIPTION
*   The smart card help function
*
* CALLED BY
*   smartcard_main/main
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*
*
* REMARK
*     None
****************************************************************************/
static void _ScHelp(void)
{
	sysprintf("\nSmart card test:\n\n");
	sysprintf("Please make sure jumper setting is consisting with following list, and insert T=0 smartcard to both smartcard slots\n");
	sysprintf("Test is passed if valid ATR could be read from smartcard\n");
	sysprintf("SW8[ON] SW9[ON]\n");
	sysprintf("SW6[OFF] SW7[OFF] SW10[OFF] SW11[OFF]\n");
    sysprintf("Press any key to continue the test...\n");
    
    sysGetChar();	
}	

/****************************************************************************
* FUNCTION
*   smartcard_main/main
*
* DESCRIPTION
*   The smart card test program main function
*
* CALLED BY
*   main
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*
*
* REMARK
*     None
****************************************************************************/
INT smartcard_main(VOID)
{
    INT nStatus;

	_ScHelp();
    
    SC_Init();

    /* Test Smart card 0 */
    nStatus = _ScTest(0);

    /* Test Smart card 1 */
    nStatus = _ScTest(1);

    SC_Exit();

    sysprintf("Smart card test Finish!\n");
    return(0);
}




/****************************************************************************
* FUNCTION
*   sc_test
*
* DESCRIPTION
*   The smart card test function
*
* CALLED BY
*   SC_Testall
*
* INPUTS
*   None
*
* OUTPUTS
*   None
*
* HISTORY
*   NAME        DATE        REMARKS
*
*
* REMARK
*     None
****************************************************************************/
static INT _ScTest(INT nCardNo)
{
    UINT8 uAtr[33];
    INT32 nAtrLength;
    INT32 nStatus;
    INT nLoop;

    nStatus = SC_Open((PVOID)nCardNo);
    if (nStatus < 0)
    {
        sysprintf("Smart card %d open failed\n\n", nCardNo);
        return(-1);
    }

    /* Get ATR */
    nStatus = SC_Ioctl(nCardNo, SC_IOC_GETATR, (UINT32)uAtr, (UINT32)&nAtrLength);
    if (nStatus < 0)
    {
        sysprintf("Smart card %d ioctl failed\n\n", nCardNo);
        return(-1);
    }

    sysPrintf("ATR:");
    for (nLoop = 0; nLoop < nAtrLength; nLoop++)
    {
        sysprintf("[%02x] ", uAtr[nLoop]);
    }

    SC_Close(nCardNo);

    sysprintf("\nSmart card %d test pass!\n\n", nCardNo);
    return(0);
}
